﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Deanery.Controller.Directories;

namespace Deanery.Controller.PersonData
{
    class ContactsController
    {
        private readonly deaneryEntities _db = new deaneryEntities();

        /// <summary>
        /// This method performs INSERT one record to DB in table Contacts
        /// PROBLEM: method returns nothing
        /// </summary>
        /// <param name="reccord">ArrayList(one item - one cell value) with data to insert</param>
        /// <param name="studentGuid">student guid for connecting contact</param> 
        public void Insert(ArrayList reccord, Guid studentGuid)
        {
            var typeContactObj = new TypeContactsController();

            var obj = new Contact
            {
                id_contacts = new Guid(),
                id_type_contacts = typeContactObj.FindGuidByName(reccord[0].ToString()),
                nv_description_contacts = reccord[1].ToString(),
                id_person = studentGuid
            };
            if (2 < reccord.Count) obj.nv_notes_contacts = reccord[2].ToString();

            _db.Contacts.AddObject(obj);
            _db.SaveChanges();
        }

        /// <summary>
        /// This method for Import
        /// performs INSERT one record to DB in table Contacts
        /// PROBLEM: method returns nothing
        /// </summary>
        /// <param name="typeGuid"></param>
        /// <param name="contact"></param>
        /// <param name="studentGuid"></param>
        public void Insert(Guid typeGuid, string contact, Guid person)
        {
            var obj = new Contact
            {
                id_contacts = Guid.NewGuid(),
                id_type_contacts = typeGuid,
                nv_description_contacts = contact,
                id_person = person
            };

            _db.Contacts.AddObject(obj);
            _db.SaveChanges();
        }

        public Guid FindGuidByContactDescription(Guid type, string contact)
        {
            var obj = (from t in _db.Contacts
                where t.id_type_contacts == type && t.nv_description_contacts == contact.Trim(' ') 
                select t.id_contacts).SingleOrDefault();
            return obj;
        }
    }
}
